﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using DA;
using Businis;

namespace qlhocsinh
{
    public partial class Frmnhapbangdiemmonhoc : Form
    {
        public Frmnhapbangdiemmonhoc()
        {
            InitializeComponent();
        }
        nhapbangdiemmonhoc_BUS bd = new nhapbangdiemmonhoc_BUS();
        bool kt;
        private void Frmnhapbangdiemmonhoc_Load(object sender, EventArgs e)
        {
            hienthicombobox();
            load();
        }

        private void load()
        {
            combo_mahk.Enabled=false;
            combo_mahocsinh.Enabled=false;
            combo_malop.Enabled=false;
            combo_mamonhoc.Enabled=false;
            btluu.Enabled = false;
            txtdiem15p.Enabled = false;
            txtdiem1tiet.Enabled = false;
            txtdiemtb.Enabled = false;
            txtdiem1tiet.Text = (0).ToString();
            txtdiem15p.Text = (0).ToString();
            txtdiemtb.Text = (0).ToString();
            dataGridView1.DataSource = bd.layallbd();
        }

        private void hienthicombobox()
        {
            combo_mahocsinh.DataSource = bd.layallhs() ;
            combo_mahocsinh.DisplayMember = "MAHS";
            combo_mahocsinh.ValueMember = "MAHS";
            combo_mamonhoc.DataSource = bd.layallmh();
            combo_mamonhoc.DisplayMember = "MAMH";
            combo_mamonhoc.ValueMember = "MAMH";
            combo_malop.DataSource = bd.layallml();
            combo_malop.DisplayMember = "MALOP";
            combo_malop.ValueMember = "MALOP";
            //combo_mahk.DataSource = bd.layallbd();
            //combo_mahk.DisplayMember = "MAHK";
            //combo_mahk.ValueMember = "MAHK";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            kt = true;
            combo_mahk.Enabled = true;
            combo_mahocsinh.Enabled = true;
            combo_malop.Enabled = true;
            combo_mamonhoc.Enabled = true;
            btluu.Enabled = true;
            txtdiem15p.Enabled = true;
            txtdiem1tiet.Enabled = true;
            txtdiemtb.Enabled = true;
            btthem.Enabled = false;
            btsua.Enabled = false;
            btxoa.Enabled = false;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int n = dataGridView1.CurrentCell.RowIndex;
                combo_mahocsinh.Text = dataGridView1.Rows[n].Cells[0].Value.ToString();
                combo_mamonhoc.Text = dataGridView1.Rows[n].Cells[1].Value.ToString();
                combo_malop.Text = dataGridView1.Rows[n].Cells[5].Value.ToString();
                combo_mahk.Text = dataGridView1.Rows[n].Cells[6].Value.ToString();
                txtdiem15p.Text = dataGridView1.Rows[n].Cells[2].Value.ToString();
                txtdiem1tiet.Text = dataGridView1.Rows[n].Cells[3].Value.ToString();
                txtdiemtb.Text = dataGridView1.Rows[n].Cells[4].Value.ToString();
                combo_mahk.Enabled = false;
                combo_mahocsinh.Enabled = false;
                combo_malop.Enabled = false;
                combo_mamonhoc.Enabled = false;
                btluu.Enabled = false;
                txtdiem15p.Enabled = false;
                txtdiem1tiet.Enabled = false;
                txtdiemtb.Enabled = false;
                btthem.Enabled = true;
                btsua.Enabled = true;
                btxoa.Enabled = true;
            }
            catch (Exception)
            {                

            }
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                BANGDIEM bdmh = new BANGDIEM();
                bdmh.MAHS = combo_mahocsinh.SelectedValue.ToString();
                bdmh.MAMH = combo_mamonhoc.SelectedValue.ToString();
                bdmh.MAHK = combo_mahk.SelectedItem.ToString();
                bdmh.MALOP=combo_malop.SelectedValue.ToString();
                DialogResult a=MessageBox.Show("Bạn có Chắc chắn Muốn xóa?", "Muốn xóa không", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (DialogResult.Yes==a)
                {
                    bd.xoa(bdmh);
                    load();
                }
            }
            catch (Exception)
            {

                MessageBox.Show("Không Xóa Được");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            kt = false;
            btthem.Enabled = false;
            btsua.Enabled = false;
            btxoa.Enabled = false;
            btluu.Enabled = true;
            combo_mahk.Enabled = true;
            combo_mahocsinh.Enabled = true;
            combo_malop.Enabled = true;
            combo_mamonhoc.Enabled = true;
            txtdiem15p.Enabled = true;
            txtdiem1tiet.Enabled = true;
            txtdiemtb.Enabled = true;
           
        }

        private void button4_Click(object sender, EventArgs e)
        {
           DialogResult a= MessageBox.Show("bạn có chắc chắn muốn thoát?","thoát",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
           if (DialogResult.OK == a)
           {
               this.Close();
           }
        }

        private void txtdiem1tiet_TextChanged(object sender, EventArgs e)
        {
            float a = -1000;
            float.TryParse(txtdiem1tiet.Text, out a);
            if (a < 0 || a > 10)
            {
                MessageBox.Show("Nhâp sai Điểm");
                txtdiem1tiet.Text = (0).ToString();
            }
            else
            {
                try
                {
                    if (string.IsNullOrEmpty(txtdiem15p.Text))
                    {
                        txtdiemtb.Text = ((0 + float.Parse(txtdiem1tiet.Text)) / 2).ToString();
                    }
                    else if (string.IsNullOrEmpty(txtdiem1tiet.Text))
                    {
                        txtdiemtb.Text = ((0 + float.Parse(txtdiem15p.Text)) / 2).ToString();
                    }
                    else if (string.IsNullOrEmpty(txtdiem15p.Text) && string.IsNullOrEmpty(txtdiem1tiet.Text))
                    {
                        txtdiemtb.Text = (0).ToString();
                    }
                    else
                    {
                        txtdiemtb.Text = ((float.Parse(txtdiem15p.Text) + float.Parse(txtdiem1tiet.Text)) / 2).ToString();
                    }
                }
                catch (Exception)
                {
                    txtdiemtb.Text = (0).ToString();
                }
            }
        }

        private void txtdiem15p_TextChanged(object sender, EventArgs e)
        {
            float a = -1000;
            float.TryParse(txtdiem15p.Text,out a);

            if (a < 0 ||a > 10)
            {
                MessageBox.Show("Nhâp sai Điểm");
                txtdiem15p.Text = (0).ToString();
            }
            else
            {
                try
                {
                    if (string.IsNullOrEmpty(txtdiem15p.Text))
                    {
                        txtdiemtb.Text = ((0 + float.Parse(txtdiem1tiet.Text)) / 2).ToString();
                    }
                    else if (string.IsNullOrEmpty(txtdiem1tiet.Text))
                    {
                        txtdiemtb.Text = ((0 + float.Parse(txtdiem15p.Text)) / 2).ToString();
                    }
                    else if (string.IsNullOrEmpty(txtdiem15p.Text) && string.IsNullOrEmpty(txtdiem1tiet.Text))
                    {
                        txtdiemtb.Text = (0).ToString();
                    }
                    else
                    {
                        txtdiemtb.Text = ((float.Parse(txtdiem15p.Text) + float.Parse(txtdiem1tiet.Text)) / 2).ToString();
                    }
                }
                catch (Exception)
                {
                    txtdiemtb.Text = (0).ToString();
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (kt == true)
            {
                BANGDIEM bdmh = new BANGDIEM();
             bdmh.MAHS = combo_mahocsinh.SelectedValue.ToString();
             bdmh.MAMH = combo_mamonhoc.SelectedValue.ToString();
             bdmh.MALOP = combo_malop.SelectedValue.ToString();
             bdmh.MAHK = combo_mahk.SelectedItem.ToString();
             if (bd.kiemtradulieutrung(bdmh) == true)
             {
                 MessageBox.Show("Học Sinh này đã tồn tại");
             }
             else
             {
                 try
                 {
                     if (txtdiem15p.Text.ToString() == "")
                     {
                         bdmh.D15 = 0;
                     }
                     else
                     {
                         bdmh.D15 = float.Parse(txtdiem15p.Text);
                     }
                     if (txtdiem1tiet.Text.ToString() == "")
                     {
                         bdmh.D1T = 0;
                     }
                     else
                     {
                         bdmh.D1T = float.Parse(txtdiem1tiet.Text);
                     }
                     bdmh.DTB = float.Parse(txtdiemtb.Text);
                     bd.them(bdmh);
                     MessageBox.Show("Thêm thành công");
                     btthem.Enabled = true;
                     btxoa.Enabled = true;
                     btsua.Enabled = true;
                     btluu.Enabled = false;
                     combo_mahk.Enabled = false;
                     combo_mahocsinh.Enabled = false;
                     combo_malop.Enabled = false;
                     combo_mamonhoc.Enabled = false;
                     txtdiem15p.Enabled = false;
                     txtdiem1tiet.Enabled = false;
                     txtdiemtb.Enabled = false;
                     load();
                 }
                 catch (Exception)
                 {
                     MessageBox.Show("không thêm được");
                 }
             }
            }
            else
            {
                try
                {
                    BANGDIEM bdmh = new BANGDIEM();
                    bdmh.MAHS = combo_mahocsinh.SelectedValue.ToString();
                    bdmh.MAMH = combo_mamonhoc.SelectedValue.ToString();
                    bdmh.MALOP = combo_malop.SelectedValue.ToString();
                    bdmh.MAHK = combo_mahk.SelectedItem.ToString();
                    if (bd.kiemtradulieutrung(bdmh) == false)
                    {
                        MessageBox.Show("Học Sinh cần Sửa không tồn tại");
                    }
                    if (txtdiem15p.Text.ToString() == "")
                    {
                        bdmh.D15 = 0;
                    }
                    else
                    {
                        bdmh.D15 = float.Parse(txtdiem15p.Text);
                    }
                    if (txtdiem1tiet.Text.ToString() == "")
                    {
                        bdmh.D1T = 0;
                    }
                    else
                    {
                        bdmh.D1T = float.Parse(txtdiem1tiet.Text);
                    }
                    bdmh.DTB = float.Parse(txtdiemtb.Text);
                    bd.sua(bdmh);
                    MessageBox.Show("Sửa Đổi Thành Công");
                    btthem.Enabled = true;
                    btsua.Enabled = true;
                    btluu.Enabled = false;
                    btxoa.Enabled = true;
                    combo_mahk.Enabled = false;
                    combo_mahocsinh.Enabled = false;
                    combo_malop.Enabled = false;
                    combo_mamonhoc.Enabled = false;
                    txtdiem15p.Enabled = false;
                    txtdiem1tiet.Enabled = false;
                    txtdiemtb.Enabled = false;
                    load();
                }
                catch (Exception)
                {
                    MessageBox.Show("loi rui");
                }
            }
        }

     
    }
}
